package com.weixin.program.service.impl;

import com.alibaba.fastjson.JSON;
import com.weixin.program.dao.RecordMapper;
import com.weixin.program.po.Record;
import com.weixin.program.service.RecordService;
import com.weixin.program.utils.HolidayUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class RecordServiceImpl implements RecordService {

    @Autowired
    RecordMapper recordMapper;


    @Override
    public Record selectByUserIdAndTime(String userId, String dateTime) {
        String amPm = HolidayUtil.getAmPm();
        Example example = new Example(Record.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("dateTime", dateTime);
        criteria.andEqualTo("amPm", amPm);
        criteria.andEqualTo("fromUserId", userId);
        return recordMapper.selectOneByExample(example);
    }

    @Override
    public void save(Map map) {
        String nowDate = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyyMMdd"));
        String amPm = HolidayUtil.getAmPm();
        String toUserName = (String) map.get("ToUserName");
        String fromUserName = (String) map.get("FromUserName");
        Record recordInfo = selectByUserIdAndTime(fromUserName, nowDate);
        if (recordInfo == null) {
            JSON mapStr = (JSON) JSON.toJSON(map);
            Record record = JSON.toJavaObject(mapStr, Record.class);
            record.setDateTime(nowDate);
            record.setCreateTime(new Date());
            record.setRecordTime(new Date());
            record.setStatus(1);
            record.setToUserId(toUserName);
            record.setFromUserId(fromUserName);
            record.setAmPm(amPm);
            recordMapper.insert(record);
        } else {
            Record record = new Record();
            record.setUpdateTime(new Date());
            String content = (String) map.get("Content");
            record.setContent(content);
            String msgId = (String) map.get("MsgId");
            record.setMsgId(Long.valueOf(msgId));
            record.setRecordTime(new Date());
            record.setStatus(1);
            Example example1 = new Example(Record.class);
            Example.Criteria criteria1 = example1.createCriteria();
            criteria1.andEqualTo("dateTime", nowDate);
            criteria1.andEqualTo("amPm", amPm);
            criteria1.andEqualTo("fromUserId", fromUserName);
            recordMapper.updateByExampleSelective(record, example1);
        }
    }

    @Override
    public void saveNotDoneData(HashMap map) {
        String nowDate = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyyMMdd"));
        String amPm = HolidayUtil.getAmPm();
        String fromUserName = (String) map.get("FromUserName");
        Record recordInfo = selectByUserIdAndTime(fromUserName, nowDate);
        if (recordInfo == null) {
            JSON mapStr = (JSON) JSON.toJSON(map);
            String toUserName = (String) map.get("ToUserName");
            String FromUserName = (String) map.get("FromUserName");
            Record record = JSON.toJavaObject(mapStr, Record.class);
            record.setDateTime(nowDate);
            record.setCreateTime(new Date());
            record.setStatus(0);
            record.setToUserId(toUserName);
            record.setFromUserId(FromUserName);
            record.setAmPm(amPm);
            recordMapper.insert(record);
        } else {
            Record record = new Record();
            record.setUpdateTime(new Date());
            Example example1 = new Example(Record.class);
            Example.Criteria criteria1 = example1.createCriteria();
            criteria1.andEqualTo("dateTime", nowDate);
            criteria1.andEqualTo("amPm", amPm);
            criteria1.andEqualTo("fromUserId", fromUserName);
            recordMapper.updateByExampleSelective(record, example1);
        }
    }

    @Override
    public List<Record> selectInSectionTime(String startDate, String currentDate, String userId) {
        Example example = new Example(Record.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andGreaterThanOrEqualTo("dateTime", startDate);
        criteria.andLessThanOrEqualTo("dateTime", currentDate);
        criteria.andEqualTo("fromUserId", userId);
        List<Record> records = recordMapper.selectByExample(example);
        // 排序
        List<Record> newRecords = records.stream().sorted(Comparator.comparing(e -> e.getDateTime())).collect(Collectors.toList());
        return newRecords;
    }
}
